/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wso.dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import wso.dto.WsoUser;
import wso.global.BaseDao;

/**
 *
 * @author Violet_Ink
 */
public class UserDaoImpl extends BaseDao implements UserDao {

    public void saveOrUpdateUser(WsoUser user) {
        saveOrUpdate(user);
    }

    public void deleteUser(String username) {
        Session session = getSession();
        Query query = null;
        StringBuilder str = new StringBuilder();
        str.append("update WsoUser user set user.isdeleted = true where user.username = :username");
        query = session.createQuery(str.toString());
        query.setString("username", username);

        query.executeUpdate();
    }

    public WsoUser[] getAllUser() {
        Session session = getSession();
        Query query = null;
        StringBuilder str = new StringBuilder();
        str.append("from WsoUser user where user.isdeleted = false");
        query = session.createQuery(str.toString());
        List userList = query.list();
        WsoUser[] result = new WsoUser[userList.size()];
        userList.toArray(result);

        return result;
    }

    public WsoUser getUser(String username) {
        Session session = getSession();
        Query query = null;
        StringBuilder str = new StringBuilder();

        str.append("from WsoUser user where user.username = :username and user.isdeleted = false");
        query = session.createQuery(str.toString());
        query.setString("username", username);

        return (WsoUser) query.uniqueResult();
    }

    public WsoUser[] getUserbyRole(String roleid) {
        Session session = getSession();
        Query query = null;
        StringBuilder str = new StringBuilder();
        str.append("from WsoUser user where user.isdeleted = false and user.roleid = :roleid");
        query = session.createQuery(str.toString());
        query.setString("roleid", roleid);

        List userList = query.list();
        WsoUser[] result = new WsoUser[userList.size()];
        userList.toArray(result);

        return result;
    }
}
